package edu.zut.lxc.loveime;


import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;


import com.lzf.easyfloat.EasyFloat;
import com.lzf.easyfloat.enums.ShowPattern;
import com.lzf.easyfloat.enums.SidePattern;
import com.lzf.easyfloat.interfaces.OnInvokeView;

import com.zhy.http.okhttp.OkHttpUtils;

import java.util.concurrent.TimeUnit;


import edu.zut.lxc.loveime.util.MyHandler;
import okhttp3.OkHttpClient;


/**
 *
 * 类说明:
 *
 * @author qing
 * @time 31/01/2018 17:59
 */
public class MyApp extends Application {

    private static MyApp sInstance;

    private MyHandler handler = null;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//        MultiDex.install(this);
    }


  private void initEasyFloat(){
      EasyFloat.init(this, true);

      registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
          @Override
          public void onActivityCreated(Activity activity, Bundle bundle) {
              Log.e("TAG111","onActivityCreated");
          }

          @Override
          public void onActivityStarted(Activity activity) {
              Log.e("TAG111","onActivityStarted");

          }

          @Override
          public void onActivityResumed(Activity activity) {
              Log.e("TAG111","onActivityResumed");
              doOnActivityResume(activity);
          }

          @Override
          public void onActivityPaused(Activity activity) {
              Log.e("TAG111","onActivityPaused");
          }

          @Override
          public void onActivityStopped(Activity activity) {
              Log.e("TAG111","onActivityStopped");

          }

          @Override
          public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
              Log.e("TAG111","onActivitySaveInstanceState");
              doOnActivityResume(activity);
          }

          @Override
          public void onActivityDestroyed(Activity activity) {
              Log.e("TAG111","onActivityDestroyed");
              doOnActivityStopped(activity);
          }
      });

  }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
//        XUtil.debug(true);


        initEasyFloat();

        // 初始化插件
//        initXHttp();
        initOKHttpUtils();
//        initXUpdate();
    }





    private static final String TAG_FLOAT_VIEW = "TAG_FLOAT_VIEW";

    private void doOnActivityResume(Activity activity) {
         EasyFloat.Builder builder = EasyFloat.with(activity)
                .setLayout(R.layout.float_window, new OnInvokeView() {
                    @Override
                    public void invoke(View view) {
                        View float_view = view.findViewById(R.id.float_view);
                        float_view.setOnClickListener(new View.OnClickListener
                        () {
                            @Override
                            public void onClick(View view) {
                                Log.e("TAG_FLOAT_VIEW","点击了悬浮窗");
                                InputMethodManager imeManager = (InputMethodManager) sInstance.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                if (imeManager != null) {
                                    Log.e("TAG_FLOAT_VIEW","再次点击了悬浮窗");
                                    imeManager.showInputMethodPicker();
                                }
                            }
                        });
                    }
                })
                .setShowPattern(ShowPattern.ALL_TIME)
                .setAnimator(null) /*关闭动画*/
                .setSidePattern(SidePattern.RESULT_HORIZONTAL) /*吸附效果*/
                .setTag(TAG_FLOAT_VIEW);

        builder.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        builder.show();
    }
    private void doOnActivityStopped(Activity activity) {
        EasyFloat.dismiss(activity, TAG_FLOAT_VIEW);//关闭前一个界面的悬浮窗
    }


    public static MyApp getInstance() {
        return sInstance;
    }



    private void initOKHttpUtils() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(20000L, TimeUnit.MILLISECONDS)
                .readTimeout(20000L, TimeUnit.MILLISECONDS)
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }


}
